package Servlet.Admin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Creche.Admin.Administrateur;
import Creche.Bd.BD;
import Form.InscriptionForm;
 
@SuppressWarnings("serial")
public class Inscription extends HttpServlet {
	
	public static final String URL_REDIRECTION = "accueilAdmin.jsp";
	public static final String VUE = "/WEB-INF/inscription.jsp";
    public static final String ADMIN = "admin";
    public static final String SESSION = "session";
    public static final String FORM = "form";
    
    
    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
    		throws ServletException, IOException {
    	
        BD myBD = new BD();
        myBD.updateCode();
    	
    	/* Affichage de la page d'inscription */
        this.getServletContext().getRequestDispatcher( VUE ).forward(req, resp);
    }
    
    
    public void doPost(HttpServletRequest req, HttpServletResponse resp) 
    		throws ServletException, IOException {
    	
    	InscriptionForm form = new InscriptionForm();
        Administrateur admin = form.inscrireAdmin(req);
        
        if (form.getErreurs().isEmpty() && !form.getInscrit()) {
        	req.getSession().setAttribute(SESSION, admin);
        	resp.sendRedirect(URL_REDIRECTION);
        }
         
        req.setAttribute(FORM, form);
        req.setAttribute(ADMIN, admin);
 
        /* Transmission de la paire d'objets request/response à notre JSP */
        this.getServletContext().getRequestDispatcher( VUE ).forward(req, resp);
    }
}
